﻿using log4net;
using System.Collections.ObjectModel;
using System.ComponentModel;

using Telerik.Windows.Controls;
using TruckScale.Models;

namespace TruckScale.Views
{
  /// <summary>
  /// Interaction logic for DMRoleView.xaml
  /// </summary>
  public partial class DMRoleView : RadWindow, INotifyPropertyChanged
  {
    //log
    private static readonly log4net.ILog log = LogManager.GetLogger("DMUserView");
    //新增后同步刷新列表
    public event PropertyChangedEventHandler PropertyChanged;
    public string InfoBar { get; set; }
    public string Theme { get; set; } = Global._weighConfig["Theme"];
    public string MainFontSize { get; set; } = Global._weighConfig["MainFontSize"];
    public ObservableCollection<RoleModel> RoleList { get; set; }
    public RoleModel SelectedRole { get; set; }
    public DMRoleView()
    {
      InitializeComponent();
      this.DataContext = this;

      RoleList = new ObservableCollection<RoleModel>(RoleModel.SelectAllRole());
    }

    private void RadGridView_Deleting(object sender, GridViewDeletingEventArgs e)
    {
      InfoBar = string.Empty;
      //foreach (UserModel user in e.Items)
      //{
      //  int ret = UserModel.SelectUsedUser(user);
      //  if (ret > 0)
      //  {
      //    InfoBar = $"{user.UserName} 有关联的称重记录，无法删除！";
      //    e.Cancel = true;
      //    return;
      //  }
      //  if (ret < 0)
      //  {
      //    InfoBar = $"删除失败，请查看系统日志！";
      //    e.Cancel = true;
      //    return;
      //  }
      //  if (ret == 0)
      //  {
      //    ret = UserModel.DeleteUser(user);
      //    if (ret > 0)
      //    {
      //      InfoBar = $"用户 {user.UserName} 已删除";
      //      log.Info(InfoBar);
      //    }
      //    else if (ret < 0)
      //    {
      //      InfoBar = $"删除失败，请查看系统日志！";
      //    }
      //    return;
      //  }
      //}
    }

    private void RadRadioButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
      InfoBar = string.Empty;
      //UserModel user = ((RadRadioButton)sender).DataContext as UserModel;

      //UserModel.UpdateUser(user);
      //if (user.IsValid)
      //{
      //  InfoBar = $"启用用户{user.UserName}";
      //  log.Info(InfoBar);
      //}
      //else
      //{
      //  InfoBar = $"禁用用户{user.UserName}";
      //  log.Info(InfoBar);
      //}

      //UserList = new ObservableCollection<UserModel>(UserModel.SelectAllUser());
    }

    private void RadButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
      InfoBar = string.Empty;
      //RadButton btn = sender as RadButton;
      //if (btn.Content.ToString() == "编辑" && SelectedUser == null)
      //{
      //  InfoBar = "请选择一条记录";
      //  return;
      //}
      //else if (btn.Content.ToString() == "新增")
      //{
      //  SelectedUser = null;
      //}

      //RadWindow window = new DMUserDetailView(SelectedUser);
      //window.Owner = this;

      //window.ShowDialog();

      //UserList = new ObservableCollection<UserModel>(UserModel.SelectAllUser());
    }
  }
}
